package com.niguang.labour.admin.service.impl;

import com.niguang.labour.admin.entity.AdminLaborUser;
import com.niguang.labour.admin.entity.WorkLaborCompany;
import com.niguang.labour.admin.mapper.AdminLaborUserMapper;
import com.niguang.labour.admin.service.AdminLaborUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.niguang.labour.admin.service.WorkLaborCompanyService;
import com.niguang.labour.common.constant.ResultCode;
import com.niguang.labour.common.pojo.Result;
import com.niguang.labour.common.util.ApiTokenUtils;
import com.niguang.labour.common.util.EncryptionMD5;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.Map;

import static com.niguang.labour.common.pojo.Result.build;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2018-06-10
 */
@Service
public class AdminLaborUserServiceImpl extends ServiceImpl<AdminLaborUserMapper, AdminLaborUser> implements AdminLaborUserService {

    @Autowired
    private WorkLaborCompanyService workLaborCompanyService;

    @Override
    public Result loginUser(String userPhone, String pwd) {
        AdminLaborUser adminLaborUser = baseMapper.selectOne(new AdminLaborUser(userPhone));
        if (ObjectUtils.isEmpty(adminLaborUser)) {
            return build(ResultCode.PARAM_ERROR, "手机号[" + userPhone + "]不存在！");
        }
        if (!StringUtils.equalsIgnoreCase(EncryptionMD5.getMD5(pwd), adminLaborUser.getUserPwd())) {
            return Result.build(ResultCode.PARAM_ERROR, "密码错误！");
        }
        WorkLaborCompany workLaborCompany = workLaborCompanyService.selectById(adminLaborUser.getLaborCompanyId());
        adminLaborUser.setLaborCompanyName(workLaborCompany.getLaborName());
        Map<String, Object> map = new HashMap(2);
        map.put("userInfo", adminLaborUser);
        map.put("token", ApiTokenUtils.createNewToken(adminLaborUser.getId(),adminLaborUser.getLaborCompanyId()));
        return Result.build(ResultCode.SUCCESS, "登录成功！", map);
    }
}
